package us.ajg0702.leaderboards.signs;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.plugin.Plugin;
import us.ajg0702.leaderboards.Main;
import us.ajg0702.leaderboards.boards.StatEntry;
import us.ajg0702.leaderboards.libs.spigot.Messages;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

public class SignManager {
    static SignManager instance;
    public YamlConfiguration cfg;
    Main pl;
    File cfgFile;
    Messages msgs;
    List<BoardSign> signs;
    HashMap<String, String> names;

    private SignManager(Main pl) {
        this.signs = new ArrayList<>();


        this.names = new HashMap<>();
        this.pl = pl;
        this.msgs = Messages.getInstance();
        Bukkit.getScheduler().runTaskLater(pl, this::reload, 1L);
        Bukkit.getScheduler().runTaskTimerAsynchronously(pl, this::updateSigns, 200L, 20L);
    }

    public static SignManager getInstance() {
        return instance;
    }

    public static SignManager getInstance(Main pl) {
        if (instance == null) {
            instance = new SignManager(pl);
        }
        return instance;
    }

    public void reload() {
        this.cfgFile = new File(this.pl.getDataFolder(), "displays.yml");
        this.cfg = YamlConfiguration.loadConfiguration(this.cfgFile);
        this.cfg.options().header("This file is for storing sign location, npcs, and other things in the plugin that might display data");
        this.signs.clear();
        if (this.cfg.contains("signs")) {
            List<String> rawsigns = this.cfg.getStringList("signs");
            for (String s : rawsigns) {
                try {
                    this.signs.add(BoardSign.deserialize(s));
                } catch (Exception e) {
                    this.pl.getLogger().warning("An error occurred while loading a sign:");
                    e.printStackTrace();
                }
            }
        }
        updateNameCache();
    }

    public List<BoardSign> getSigns() {
        return this.signs;
    }

    public boolean removeSign(Location l) {
        boolean save = false;
        Iterator<BoardSign> i = this.signs.iterator();
        while (i.hasNext()) {
            BoardSign s = i.next();
            if (l.equals(s.getLocation())) {
                i.remove();
                save = true;
                s.setText("", "", "", "");
                break;
            }
        }
        if (save) saveFile();
        return save;
    }

    public void updateNameCache() {
        List<String> namesraw = this.pl.getAConfig().getStringList("value-names");
        for (String s : namesraw) {
            if (!s.contains("%"))
                continue;
            String[] parts = s.split("%");
            this.names.put(parts[0], parts[1]);
        }
    }

    public BoardSign findSign(Location l) {
        Iterator<BoardSign> i = this.signs.iterator();
        while (i.hasNext()) {
            BoardSign s = i.next();
            if (l.equals(s.getLocation())) return s;
        }
        return null;
    }

    public void addSign(Location loc, String board, int pos) {
        if (findSign(loc) != null) return;
        this.signs.add(new BoardSign(loc, board, pos));
        saveFile();
    }

    public void saveFile() {
        List<String> signsraw = new ArrayList<>();
        for (BoardSign sign : this.signs) signsraw.add(sign.serialize());
        this.cfg.set("signs", signsraw);
        try {
            this.cfg.save(this.cfgFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void updateSigns() {
        updateNameCache();
        for (BoardSign sign : this.signs)
            updateSign(sign);
    }

    public void updateSign(BoardSign sign) {
        if (!isSignChunkLoaded(sign))
            return;
        String name = "";
        if (this.names.containsKey(sign.getBoard())) {
            name = this.names.get(sign.getBoard());
        }

        StatEntry r = TopManager.getInstance().getStat(sign.getPosition(), sign.getBoard());

        List<String> lines = Arrays.asList(new String[]{this.msgs
                .get("signs.top.1"), this.msgs
                .get("signs.top.2"), this.msgs
                .get("signs.top.3"), this.msgs
                .get("signs.top.4")});
        List<String> plines = new ArrayList<>();
        for (String l : lines) {


            String pline = l.replaceAll("\\{POSITION}", sign.getPosition() + "").replaceAll("\\{NAME}", r.getPlayer()).replaceAll("\\{VALUE}", r.getScorePretty()).replaceAll("\\{VALUENAME}", name);

            plines.add(pline);
        }

        Bukkit.getScheduler().runTask(this.pl, () -> {
            if (!r.getPlayer().equals(this.pl.getAConfig().getString("no-data-name")) && r.getPlayerID() != null) {
                HeadManager.getInstance().search(sign, r.getPlayer(), r.getPlayerID());
            }
            sign.setText(plines.get(0), plines.get(1), plines.get(2), plines.get(3));
        });
    }


    public boolean isSignChunkLoaded(BoardSign sign) {
        return sign.getWorld().isChunkLoaded(sign.getX(), sign.getZ());
    }
}


*Location:D:\User\Xuan\Downloads\ajLeaderboards-1.3.3.jar\\us\ajg0702\leaderboards\signs\SignManager.class
 *Java compiler version:8(52.0)
         *JD-Core Version:1.1.3
